﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{   
    /*{
"favorited":"null",
"text":"test",
"in_reply_to_user_id":"null",
"created_at":"Tue May 11 11:54:22 +0200 2010",
"created_at_iso8601":"2010-05-11T11:54:22+02:00",
"truncated":"false",
"source":"http:\/\/utipistes.hellotipi.com",
"id":"439433",

"user":{
	"id":"3651",
	"name":"Christophe Rous\u00e9e",
	"screen_name":"ptirouz#utipistes",
	"location":"",
	"description":"",
	"profile_image_url":"http:\/\/img.hellotipi.com\/gallery\/user\/3651_56301b36a7.jpg",
	"url":"http:\/\/utipistes.hellotipi.com\/?page=profil&action=view_one&login=ptirouz",
	"protected":"false",
	"followers_count":0
	},

"site":{
	"name":"utipistes",
	"title":null
	}
}
*/
    /// <summary>Objet du résultat d'une requête d'envoie de message public</summary>
    /// <see cref="update"></see>
    [DataContract]
    public class update_public
    {

        /// <summary>non utilisé</summary>
        //[DataMember]
        //public bool? favorited { get; set; }

        /// <summary>texte du message</summary>
        [DataMember]
        public string text { get; set; }

        /// <summary>non utilisé</summary>
        //[DataMember]
        //public int? in_reply_to_user_id { get; set; }

        /// <summary>date du message au format suivant : Tue Sep 16 11:38:39 +0200 2008 </summary>
        /// <remarks>en PHP : date(D M j H:i:s O Y</remarks>
        [DataMember]
        public string created_at { get; set; }

        /// <summary>date du message au format suivant : 2010-05-11T11:54:22+02:00 </summary>
        [DataMember]
        public string created_at_iso8601 { get; set; }

        /// <summary>non utilisé</summary>
        //[DataMember]
        //public bool truncated { get; set; }

        /// <summary>url du site de famille</summary>
        [DataMember]
        public string source { get; set; }

        /// <summary>id du message</summary>
        [DataMember]
        public user user { get; set; }

        /// <summary>expéditeur</summary>        
        [DataMember]
        public site site { get; set; }


    }

    /// <summary>Objet du résultat d'une requête d'envoie de message privée</summary>
    /// <see cref="update"></see>
    [DataContract]
    public class update_private
    {

        /// <summary>texte du message</summary>
        [DataMember]
        public string text { get; set; }

        /// <summary>ID de l’expéditeur</summary>
        [DataMember]
        public int sender_id { get; set; }

        /// <summary>ID du destinataire</summary>
        [DataMember]
        public int recipient_id { get; set; }

        /// <summary>date du message au format suivant : Tue Sep 16 11:38:39 +0200 2008 </summary>
        /// <remarks>en PHP : date(D M j H:i:s O Y</remarks>
        [DataMember]
        public string created_at { get; set; }

        /// <summary>login de l’expéditeur suivi de sa famille</summary>
        [DataMember]
        public string sender_screen_name { get; set; }

        /// <summary>login du destinataire suivi de sa famille</summary>
        [DataMember]
        public string recipient_screen_name { get; set; }

        /// <summary>expéditeur</summary>        
        [DataMember]
        public user sender { get; set; }

        /// <summary>destinataire</summary>                
        [DataMember]
        public user recipient { get; set; } 
    }

    /* Public */
    public delegate void UpdatePublicEventHandler(object sender, UpdatePublicEventArgs e);
    public class UpdatePublicEventArgs : EventArgs
    {
        public UpdatePublicEventArgs(update_public up)
        {
            _result = up;
        }

        private update_public _result;
        public update_public result { get { return _result; } }
    }

    /* Privée */
    public delegate void UpdatePrivateEventHandler(object sender, UpdatePrivateEventArgs e);
    public class UpdatePrivateEventArgs : EventArgs
    {
        public UpdatePrivateEventArgs(update_private up)
        {
            _result = up;
        }

        private update_private _result;
        public update_private result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event UpdatePublicEventHandler UpdatePublicCompleted;
        public event UpdatePrivateEventHandler UpdatePrivateCompleted;

        #region update public
        /// <summary>Permet de poster un nouveau message public ou privé dont l’expéditeur est l’utilisateur authentifié.</summary>
        /// <c>http://www.hellotipi.com/dev/documentation/fonctions-messages-textes/#statuses/update</c>
        /// <param name="status">string texte du message, 500 caractères maxi, requis</param>
        /// <param name="in_reply_to_status_id ">string non utlisé, optionnel</param>
        /// <returns>La liste des photos de l'album <paramref name="album"/> du site de famille <paramref name="site"/></returns>
        public void UpdatePublic(string status, string in_reply_to_status_id = "")
        {

            base.APICallCompleted += new APICallCompletedEventHandler(UpdatePublicCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("status", status),
                                 new KeyValuePair<string, string>("source", this.appSource)
                             };

            if (in_reply_to_status_id != String.Empty)
                querystring.Add(new KeyValuePair<string, string>("in_reply_to_status_id", in_reply_to_status_id));

            GetUrlApi("statuses/update.json", querystring);
        }

        void UpdatePublicCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                    UpdatePublicCompleted.Invoke(sender, new UpdatePublicEventArgs(serializeTools.Deserialize<update_public>(e.Result)));
            } 
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(UpdatePublicCallCompleted);
            }
        }

        #endregion

        #region update private
        /// <summary>Permet de poster un nouveau message public ou privé dont l’expéditeur est l’utilisateur authentifié.</summary>
        /// <c>http://www.hellotipi.com/dev/documentation/fonctions-messages-textes/#statuses/update</c>
        /// <param name="status">string texte du message, 500 caractères maxi, requis</param>
        /// <param name="in_reply_to_status_id ">string non utlisé, optionnel</param>        
        public void UpdatePrivate(string status, string in_reply_to_status_id = "")
        {

            base.APICallCompleted += new APICallCompletedEventHandler(UpdatePrivateCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("status", status),
                                 new KeyValuePair<string, string>("in_reply_to_status_id", in_reply_to_status_id),
                                 new KeyValuePair<string, string>("source", this.appSource)
                             };

            GetUrlApi("statuses/update.json", querystring);
        }

        void UpdatePrivateCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                UpdatePrivateCompleted.Invoke(sender, new UpdatePrivateEventArgs(serializeTools.Deserialize<update_private>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(UpdatePrivateCallCompleted);
            }
        }

        #endregion
    }

}

